﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace HookupAppForm.Draw
{
    public partial class UC_DrawEdit
    {
        private bool isMouseDown = false;
        private Point mouseDownPoint = new Point();
        private double mouseDownImgMoveX = 0;
        private double mouseDownImgMoveY = 0;
        public double ImgLocN { get; set; }
        public double ImgLocS { get; set; }
        public double ImgLocW { get; set; }
        public double ImgLocE { get; set; }
        private Point lastMovePt = new Point();


        private double _ImgScaleX = 1;
        public double ImgScaleX
        {
            get { return _ImgScaleX; }
            set
            {
                _ImgScaleX = value;
                cc("ImgScaleX");
            }
        }
        private double _ImgScaleY = 1;
        public double ImgScaleY
        {
            get { return _ImgScaleY; }
            set
            {
                _ImgScaleY = value;
                cc("ImgScaleY");
            }
        }
        private double _ImgMoveX = 0;
        public double ImgMoveX
        {
            get { return _ImgMoveX; }
            set
            {
                _ImgMoveX = value;
                cc("ImgMoveX");
            }
        }
        private double _ImgMoveY = 0;
        public double ImgMoveY
        {
            get { return _ImgMoveY; }
            set
            {
                _ImgMoveY = value;
                cc("ImgMoveY");
            }
        }

        private double ImgWidthInt
        {
            get
            {
                return (double)(cvDrawBg.RenderSize.Width);
            }
        }

        private double ImgHeightInt
        {
            get
            {
                return (double)(cvDrawBg.RenderSize.Height);
            }
        }

        private double _CurrWD = 0;
        public double CurrWD
        {
            get { return _CurrWD; }
            set
            {
                _CurrWD = value;
                cc("CurrWD");
            }
        }

        private double _CurrJD = 0;
        public double CurrJD
        {
            get { return _CurrJD; }
            set
            {
                _CurrJD = value;
                cc("CurrJD");
            }
        }





        private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (CurrDrawCommand != DrawCommand.缩放) return;


            if (imgbg.VerticalAlignment == VerticalAlignment.Center
                && imgbg.HorizontalAlignment == HorizontalAlignment.Center)
            {
                var ePoint = e.GetPosition((sender as UIElement));

                //鼠标位于图片的百分比，在缩放后，仍然处于相同百分比
                var showWidth = ImgWidthInt * ImgScaleX;
                var showHeight = ImgHeightInt * ImgScaleY;
                var ePointXScale = (-ImgMoveX + ePoint.X) / showWidth;
                var ePointYScale = (-ImgMoveY + ePoint.Y) / showHeight;


                //缩放，每次缩放当前图的指定比例
                if (e.Delta > 0)
                {
                    ImgScaleX += ImgScaleX * 0.1;
                    ImgScaleY += ImgScaleY * 0.1;
                }
                else
                {
                    if (ImgScaleX < 0.1) return;

                    ImgScaleX -= ImgScaleX * 0.1;
                    ImgScaleY -= ImgScaleY * 0.1;
                }

                //还原鼠标位置所占大图比例
                showWidth = ImgWidthInt * ImgScaleX;
                showHeight = ImgHeightInt * ImgScaleY;
                ImgMoveX = -(ePointXScale * showWidth - ePoint.X);
                ImgMoveY = -(ePointYScale * showHeight - ePoint.Y);

                //refreshAllTags();
            }
        }

        private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (CurrDrawCommand != DrawCommand.缩放) return;

            isMouseDown = true;
            mouseDownPoint = e.GetPosition((sender as UIElement));
            mouseDownImgMoveX = ImgMoveX;
            mouseDownImgMoveY = ImgMoveY;
        }
        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isMouseDown || e.LeftButton != MouseButtonState.Pressed) return;
            var currPoint = e.GetPosition((sender as UIElement));
            var xMove = currPoint.X - mouseDownPoint.X;
            var yMove = currPoint.Y - mouseDownPoint.Y;
            ImgMoveX = mouseDownImgMoveX + xMove;
            ImgMoveY = mouseDownImgMoveY + yMove;

            //refreshAllTags();
        }

        private void ImgCnt_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            lastMovePt = new Point(CurrJD, CurrWD);
        }

        /// <summary>
        /// 及时更新位置信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Img_MouseMove(object sender, MouseEventArgs e)
        {
            var img = sender as Image;
            var pt = e.GetPosition(img);
            var imgWidth = ImgWidthInt;
            var imgHeight = ImgHeightInt;

            CurrJD = ImgLocW + (ImgLocE - ImgLocW) * (pt.X / imgWidth);
            CurrWD = ImgLocS + (ImgLocN - ImgLocS) * ((imgHeight - pt.Y) / imgHeight);
        }

        private void btn_showmapall_click(object sender, RoutedEventArgs e)
        {
            var scl_1 = cvdraw_container.ActualWidth / cvDrawMain.ActualWidth;
            var scl_2 = cvdraw_container.ActualHeight / cvDrawMain.ActualHeight;
            var scltmp = Math.Min(scl_1, scl_2);

            ImgScaleX = scltmp;
            ImgScaleY = scltmp;

            ImgMoveX = (cvdraw_container.ActualWidth - cvDrawMain.ActualWidth * ImgScaleX) / 2;
            ImgMoveY = (cvdraw_container.ActualHeight - cvDrawMain.ActualHeight * ImgScaleY) / 2;

      
        }
    }
}
